package com.android.svmc.app.mobile.activity;

import java.util.ArrayList;

import luyenlv.pro.screen.base.BaseActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.svmc.app.mobile.adapter.DiaryListAdapter;
import com.android.svmc.app.mobile.controller.SwipeDismissListViewTouchListener;
import com.android.svmc.app.mobile.data.DatabaseSource;
import com.android.svmc.app.mobile.model.DiaryModel;
import com.android.svmc.app.mobile.tabmanager.TabGroupActivity;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.ConfigModel;


public class DiaryListXL extends BaseActivity implements OnClickListener,CommonDefine{
	TabGroupActivity parentActivity;
	private ListView gdview_listdiary;
	private TextView txt_wellcome;
	private Button addnew;
	private ArrayList<DiaryModel> listDiary;
	DiaryListAdapter myAdaper;
	private DatabaseSource db;
	private String username;
	String [] data = {"anh son","hoang toan","Hong phong"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diary_list_xl_320);
	    parentActivity = (TabGroupActivity) getParent();
	    db = new DatabaseSource(this);
	    /**
	     * Get data intent
	     */
	    Bundle data = getIntent().getExtras();
	    username = data.getString("username");
	    
	    /*********************/
	    initUI();
	    setClickListen();
	}
	private void initUI(){
		gdview_listdiary = (ListView)findViewById(R.id.diary_list_listview);
		txt_wellcome = (TextView)findViewById(R.id.diary_list_xl_txt_wellcome);
		addnew =(Button)findViewById(R.id.diary_list_xl_btn_add);
		txt_wellcome.setText("Xin chào " +username);
	}
	private void setClickListen(){
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateAllDiary();
		if(ConfigModel.getApiLevel()>=11){
			swipeListview();
		}else {
			showContextmenu();
		}
		
	}
	@Override
	public void onClick(View v) {
		
		
	}
	public void updateAllDiary (){
		listDiary = db.getDiaryByAccountName(username);
		myAdaper = new DiaryListAdapter(this, listDiary);
		gdview_listdiary.setAdapter(myAdaper);
	}

	private void showContextmenu() {
		
		
	}
	@Override
	public void clearMemory() {
		
		
	}
	/**
	 * ListView slide left/right like Samsung contact ListView
	 */
	public void swipeListview (){
		SwipeDismissListViewTouchListener  touchListener =
        	    new SwipeDismissListViewTouchListener(
            	        gdview_listdiary,
            	        new SwipeDismissListViewTouchListener.OnSwipeCallback() {
            	            @Override
            	            public void onSwipeLeft(ListView listView, int [] reverseSortedPositions) {
            	                  Log.i(this.getClass().getName(), "swipe left : pos="+reverseSortedPositions[0]);
            	                // TODO : YOUR CODE HERE FOR LEFT ACTION
            	            	//deleteOneTask(listtask.get(reverseSortedPositions[0]));
            	                Toast.makeText(DiaryListXL.this, "Delete", 0).show();
            	                
            	            	db.deleteDiary(listDiary.get(reverseSortedPositions[0]));
            	            	listDiary.remove(reverseSortedPositions[0]);
            	            	myAdaper.notifyDataSetChanged();
            	            	            	
            	            }

            	            @Override
            	            public void onSwipeRight(ListView listView, int [] reverseSortedPositions) {
            	                 Log.i(this.getClass().getName(), "swipe right : pos="+reverseSortedPositions[0]);
            	                // TODO : YOUR CODE HERE FOR RIGHT ACTION
            	            	//deleteOneTask(listtask.get(reverseSortedPositions[0]));
            	                
            	            }

							@Override
							public void onDismiss(ListView listView,
									int[] reverseSortedPositions) {
								// TODO Auto-generated method stub
								Toast.makeText(DiaryListXL.this, "Delete all", 0).show();
							}
            	        },
            	        true, // example : left action = dismiss
            	        false); // example : right action without dismiss animation;
		gdview_listdiary.setOnTouchListener(touchListener);
        // Setting this scroll listener is required to ensure that during ListView scrolling,
        // we don't look for swipes.
        gdview_listdiary.setOnScrollListener(touchListener.makeScrollListener());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		        // do something on back.
			 Toast.makeText(this, "back key", 0).show();
		        return true;
		    }

		    return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//super.onBackPressed();
		 android.os.Process.killProcess(android.os.Process.myPid());
		
	}

}
